using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class AnimatorVFXPlayer : StateMachineBehaviour
    {
        private static string playingGroup;

        private BehaviorFeatureComp _corePlayer;
        private float delayTimer;

        public string configGroup;
        public float configDelay;

        public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            if (_corePlayer == null)
            {
                _corePlayer = animator.transform.parent.GetComponent<BehaviorFeatureComp>();
            }
            delayTimer = 0;
        }

        public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            if (_corePlayer == null)
            {
                return;
            }
            if (delayTimer == -1)
            {
                return;
            }
            delayTimer += Time.deltaTime;
            if (delayTimer >= configDelay)
            {
                _corePlayer.PlayGroup(configGroup);
                delayTimer = -1;
            }
        }

        public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            if (_corePlayer == null)
            {
                return;
            }
            _corePlayer.StopAndRestore();
        }
    }
}
